<?PHP
/* $Id: stats.php 7 2009-03-24 20:25:52Z ronan $ */

session_start();
//getting the user's access to see if they are allowed
include("includes/paxsql.php");
mysql_close();

include 'includes/database.php';
include 'includes/library.php';

function renderStatsNavi() {
	if ($_GET['stat'] == 1 || !isset($_GET['stat'])) echo '<a href="?stat=1"><b>Dreadnoughts</b></a>';
	else echo '<a href="?stat=1">Dreadnoughts</a>';
	echo ' | ';
	if ($_GET['stat'] == 2) echo '<a href="?stat=2"><b>Carriers</b></a>';
	else echo '<a href="?stat=2">Carriers</a>';
	echo ' | ';
	if ($_GET['stat'] == 3) echo '<a href="?stat=3"><b>POS Gunners</b></a>';
	else echo '<a href="?stat=3">POS Gunners</a>';
	echo ' | ';
	if ($_GET['stat'] == 4) echo '<a href="?stat=4"><b>TZ Euro</b></a>';
	else echo '<a href="?stat=4">TZ Euro</a>';
	echo ' | ';
	if ($_GET['stat'] == 5) echo '<a href="?stat=5"><b>TZ American</b></a>';
	else echo '<a href="?stat=5">TZ American</a>';
	echo ' | ';
	if ($_GET['stat'] == 6) echo '<a href="?stat=6"><b>TZ Oceanic</b></a>';
	else echo '<a href="?stat=6">TZ Oceanic</a>';
}

function listDread() {
	$query = "SELECT alts.charID as aID, characters.charID, characters.name, characters.lastSeen, characters.inactive, characters.dread
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE characters.inCorp = 1
		AND characters.dread > 0
		ORDER BY characters.name";
		//WHERE alts.charID IS NULL
	$results = mysql_query($query);
	$numDreads = mysql_num_rows($results);
	
	echo "There are $numDreads dreadnoughts.<br><br>";
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Dreadnought</b></td></tr>";
	
	while ($char = mysql_fetch_array($results)) {
		/*** RENDER NAME ***/
		if ($char['inactive'] == 1)
			echo "<tr style=\"background: #ffa9a9;\">";
		elseif(!empty($char['aID']))
			echo "<tr style=\"background: #66ff99;\">";
		elseif (eveTime() - $char['lastSeen'] > 60*60*24*15)
			echo "<tr style=\"background: #fdff5e;\">";
		elseif (eveTime() - $char['joined'] < 60*60*24*30)
			echo "<tr style=\"background: #b9f0b9;\">";
		else
			echo "<tr>";
			
		echo "<td class=\"main\">";
		if(!empty($char['aID']))
			echo('(alt)');
		renderName($char['charID']);
		echo "</td>";
		
		/*** RENDER LAST SEEN ***/
		echo "<td class=\"main\">";
		switch ($char['dread']) {
			case 1:		echo "Revelation"; break;
			case 2:		echo "Phoenix"; break;
			case 3:		echo "Moros"; break;
			case 4:		echo "Naglfar"; break;
		}
		echo "</td></tr>";
	}
	echo "</table>";
}

function listCarrier() {
	$query = "SELECT alts.charID as aID, characters.charID, characters.name, characters.lastSeen, characters.inactive, characters.carrier
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE characters.inCorp = 1
		AND characters.carrier > 0
		ORDER BY characters.name";
		//WHERE alts.charID IS NULL
	$results = mysql_query($query);
	$numCarriers = mysql_num_rows($results);
	
	echo "There are $numCarriers carriers.<br><br>";
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Carrier</b></td></tr>";
	
	while ($char = mysql_fetch_array($results)) {
		/*** RENDER NAME ***/
		if ($char['inactive'] == 1)
			echo "<tr style=\"background: #ffa9a9;\">";
		elseif(!empty($char['aID']))
			echo "<tr style=\"background: #66ff99;\">";
		elseif (eveTime() - $char['lastSeen'] > 60*60*24*15)
			echo "<tr style=\"background: #fdff5e;\">";
		elseif (eveTime() - $char['joined'] < 60*60*24*30)
			echo "<tr style=\"background: #b9f0b9;\">";
		else
			echo "<tr>";
			
		echo "<td class=\"main\">";
		if(!empty($char['aID']))
			echo('(alt)');
		renderName($char['charID']);
		echo "</td>";
		
		/*** RENDER LAST SEEN ***/
		echo "<td class=\"main\">";
		switch ($char['carrier']) {
			case 1:		echo "Archon"; break;
			case 2:		echo "Chimera"; break;
			case 3:		echo "Thanatos"; break;
			case 4:		echo "Nidhoggur"; break;
		}
		echo "</td></tr>";
	}
	echo "</table>";
}

function listPOSGunners() {
	$query = "SELECT alts.charID as aID, characters.charID, characters.name, characters.lastSeen, characters.inactive, characters.posgunner
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE characters.inCorp = 1
		AND characters.posgunner = 1
		ORDER BY characters.name";
		//WHERE alts.charID IS NULL
	$results = mysql_query($query);
	$numTZEuro = mysql_num_rows($results);
	
	echo "There are $numTZEuro POS gunners.<br><br>";
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Last seen</b></td></tr>";
	
	while ($char = mysql_fetch_array($results)) {
		/*** RENDER NAME ***/
		if ($char['inactive'] == 1)
			echo "<tr style=\"background: #ffa9a9;\">";
		elseif(!empty($char['aID']))
			echo "<tr style=\"background: #66ff99;\">";
		elseif (eveTime() - $char['lastSeen'] > 60*60*24*15)
			echo "<tr style=\"background: #fdff5e;\">";
		elseif (eveTime() - $char['joined'] < 60*60*24*30)
			echo "<tr style=\"background: #b9f0b9;\">";
		else
			echo "<tr>";
			
		echo "<td class=\"main\">";
		if(!empty($char['aID']))
			echo('(alt)');
		renderName($char['charID']);
		echo "</td>";
		
		/*** RENDER LAST SEEN ***/
		echo "<td class=\"main\">";
		$timePassed = eveTime() - $char['lastSeen'];
		
		if ($timePassed < 3600) echo "-" . round($timePassed/60) . " mins";
		else if ($timePassed < 172800) echo "-" . round($timePassed/3600, 2) . " hrs";
		else echo "-" . round($timePassed/86400, 2) . " days";	
		echo "</td></tr>";
	}
	echo "</table>";
}

function listTZEuro() {
	$query = "SELECT characters.charID, characters.name, characters.lastSeen, characters.inactive, characters.carrier
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE alts.charID IS NULL
		AND characters.inCorp = 1
		AND characters.tz = 1
		ORDER BY characters.name";
	$results = mysql_query($query);
	$numTZEuro = mysql_num_rows($results);
	
	echo "There are $numTZEuro European TZ players.<br><br>";
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Last seen</b></td></tr>";
	
	while ($char = mysql_fetch_array($results)) {
		/*** RENDER NAME ***/
		if ($char['inactive'] == 1)
			echo "<tr style=\"background: #ffa9a9;\">";
		elseif (eveTime() - $char['lastSeen'] > 60*60*24*15)
			echo "<tr style=\"background: #fdff5e;\">";
		elseif (eveTime() - $char['joined'] < 60*60*24*30)
			echo "<tr style=\"background: #b9f0b9;\">";
		else
			echo "<tr>";
			
		echo "<td class=\"main\">";
		renderName($char['charID']);
		echo "</td>";
		
		/*** RENDER LAST SEEN ***/
		echo "<td class=\"main\">";
		$timePassed = eveTime() - $char['lastSeen'];
		
		if ($timePassed < 3600) echo "-" . round($timePassed/60) . " mins";
		else if ($timePassed < 172800) echo "-" . round($timePassed/3600, 2) . " hrs";
		else echo "-" . round($timePassed/86400, 2) . " days";	
		echo "</td></tr>";
	}
	echo "</table>";
}

function listTZAmerican() {
	$query = "SELECT characters.charID, characters.name, characters.lastSeen, characters.inactive, characters.carrier
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE alts.charID IS NULL
		AND characters.inCorp = 1
		AND characters.tz = 2
		ORDER BY characters.name";
	$results = mysql_query($query);
	$numTZAmericas = mysql_num_rows($results);
	
	echo "There are $numTZAmericas American TZ players.<br><br>";
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Last seen</b></td></tr>";
	
	while ($char = mysql_fetch_array($results)) {
		/*** RENDER NAME ***/
		if ($char['inactive'] == 1)
			echo "<tr style=\"background: #ffa9a9;\">";
		elseif (eveTime() - $char['lastSeen'] > 60*60*24*15)
			echo "<tr style=\"background: #fdff5e;\">";
		elseif (eveTime() - $char['joined'] < 60*60*24*30)
			echo "<tr style=\"background: #b9f0b9;\">";
		else
			echo "<tr>";
			
		echo "<td class=\"main\">";
		renderName($char['charID']);
		echo "</td>";
		
		/*** RENDER LAST SEEN ***/
		echo "<td class=\"main\">";
		$timePassed = eveTime() - $char['lastSeen'];
		
		if ($timePassed < 3600) echo "-" . round($timePassed/60) . " mins";
		else if ($timePassed < 172800) echo "-" . round($timePassed/3600, 2) . " hrs";
		else echo "-" . round($timePassed/86400, 2) . " days";	
		echo "</td></tr>";
	}
	echo "</table>";
}

function listTZOceanic() {
	$query = "SELECT characters.charID, characters.name, characters.lastSeen, characters.inactive, characters.carrier
		FROM characters LEFT JOIN alts
		ON characters.charID = alts.charID
		WHERE alts.charID IS NULL
		AND characters.inCorp = 1
		AND characters.tz = 3
		ORDER BY characters.name";
	$results = mysql_query($query);
	$numTZOceanic = mysql_num_rows($results);
	
	echo "There are $numTZOceanic Oceanic TZ players.<br><br>";
	
	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Last seen</b></td></tr>";
	
	while ($char = mysql_fetch_array($results)) {
		/*** RENDER NAME ***/
		if ($char['inactive'] == 1)
			echo "<tr style=\"background: #ffa9a9;\">";
		elseif (eveTime() - $char['lastSeen'] > 60*60*24*15)
			echo "<tr style=\"background: #fdff5e;\">";
		elseif (eveTime() - $char['joined'] < 60*60*24*30)
			echo "<tr style=\"background: #b9f0b9;\">";
		else
			echo "<tr>";
			
		echo "<td class=\"main\">";
		if(!empty($char['aID']))
			echo('(alt)');
		renderName($char['charID']);
		echo "</td>";
		
		/*** RENDER LAST SEEN ***/
		echo "<td class=\"main\">";
		$timePassed = eveTime() - $char['lastSeen'];
		
		if ($timePassed < 3600) echo "-" . round($timePassed/60) . " mins";
		else if ($timePassed < 172800) echo "-" . round($timePassed/3600, 2) . " hrs";
		else echo "-" . round($timePassed/86400, 2) . " days";	
		echo "</td></tr>";
	}
	echo "</table>";
}

if($access > 1)
{
echo("
<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css'>
<title>snowflake</title>");

include('includes/javascript.php');
echo("
</head>

<body>

<div id='isAltWin' style='display: none;'></div>
<div id='flagWin' style='display: none;'></div>

<div id='wrapper'>

	<div id='header'><a href='/'>snowflake</a></div>

	<div id='navi'>");

include('includes/navi.php');
echo("</div>

	<div id='main'>
		<h1>statistics</h1>
		<hr>");

renderStatsNavi();
echo("<br><br>");
 
	if ($_GET['stat'] == 2) listCarrier();
	else if ($_GET['stat'] == 3) listPOSGunners();
	else if ($_GET['stat'] == 4) listTZEuro();
	else if ($_GET['stat'] == 5) listTZAmerican();
	else if ($_GET['stat'] == 6) listTZOceanic();
	else listDread();
echo("
	</div>
	
</div>

</body>

</html>");
}
else
{
	header("Location: http://".$_SERVER['HTTP_HOST']."/index.php");
}
?>
